<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 公告管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>新增公告</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
                    <el-form-item label="标题" prop="title">
                        <el-input v-model="form.title"></el-input>
                    </el-form-item>
                    <el-form-item label="内容" prop="content">
                        <el-input type="textarea" rows="5" v-model="form.content"></el-input>
                    </el-form-item>
                    <el-form-item label="状态" prop="state">
                        <el-radio-group v-model="form.state">
                            <el-radio label="启用" name="1"></el-radio>
                            <el-radio label="关闭" name="0"></el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="封面" prop="url">
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:8888/api/admin/file/upload"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :on-remove="handleRemove"
                                :before-upload="beforeAvatarUpload"
                                limit="1"
                                on-exceed="onExceed">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">新增公告</el-button>
                        <el-button @click="onReset">清空表单</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { addNotice } from "../../api";
export default {
    name: "noticeForm",
    setup() {
        const rules = {
            title: [
                { required: true, message: "请输入公告标题", trigger: "blur" },
            ],
            content: [
                { required: true, message: "请输入公告内容", trigger: "blur" },
            ],
            state: [
                { required: true, message: "请选择是否发布", trigger: "blur" },
            ]
        };
        const formRef = ref(null);
        const imageUrl = ref(null);
        // 表单数据
        let form = reactive({
            title: '',
            content: '',
            state: null,
            url: ''
        });

        // 重置
        const onReset = () => {
            formRef.value.resetFields();
            imageUrl.value = null;
        };

        const onExceed= () => {
            ElMessage.error('只能上传一个封面');
        };

        // 提交
        const onSubmit = () => {
            // 表单校验
            formRef.value.validate((valid) => {
                if (valid) {
                    console.log(form);
                    if (form.state === '启用') form.state = ref(1);
                    else form.state = ref(0);
                    addNotice(form).then(res => {
                        if (res.code === 200) {
                            onReset();
                            ElMessage.success('添加成功');
                        } else {
                            console.log("[添加公告] 失败: " + res.message);
                            ElMessage.error('服务器繁忙，请稍后重试');
                        }
                    })
                } else {
                    return false;
                }
            });
        };

        const handleAvatarSuccess = (res, file) => {
            if (res.code !== 200) {
                ElMessage.error(rec.message);
                return ;
            }
            imageUrl.value = URL.createObjectURL(file.raw);
            form.url = ref(res.data);
        };

        const handleRemove = () => {
            imageUrl.value = null;
        };

        const beforeAvatarUpload = (file) => {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 20;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 20MB!');
            }
            return isJPG && isLt2M;
        };

        return {
            rules,
            formRef,
            form,
            imageUrl,
            onSubmit,
            onReset,
            handleAvatarSuccess,
            beforeAvatarUpload,
            handleRemove,
            onExceed
        };
    },
};
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        width: 300px;
        height: 225px;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 225px;
        text-align: center;
    }
    .avatar {
        width: 300px;
        height: 225px;
        display: block;
    }
</style>